class Solution {
public:
    bool checkPossibility(vector<int>& nums) {
        int n=nums.size(),i=0,j=n-1;
        while(i<j && nums[i]<=nums[i+1]) i++;
        while(i<j && nums[j]>=nums[j-1]) j--;
        int head=i==0?INT_MIN:nums[i-1];
        int tail=j==n-1?INT_MAX:nums[j+1];
        if(j-i<=1 && (head<=nums[j] || nums[i]<=tail)) return true;
        else return false;
    }
};

